#include"Era.h"
#include<time.h>
#include<iostream>
#include<algorithm>
#include<functional>
#include"VirtualCity.h"

bool cmp_ct_attd_up(const City& c1, const City& c2){	return c1.attd < c2.attd;	}

bool operator<(City a, City b) { return a.popu < b.popu; }
bool operator>(City a, City b) { return a.attd > b.attd; }

int main() {
	srand((unsigned)time(NULL));
	ofstream fout;

	Era era[10];

	// task1
	cout << "task1......";
	era[0].gen();
	era[0].save("City.txt");
	cout << "done!" << endl;

	// task2
	cout << "task2......";
	for (int i = 0; i < 9; i++) {
		era[i].save("City201" + to_string(i) + ".txt");
		era[i + 1] = era[i];
		era[i + 1].evo();
	}
	era[9].save("City2019.txt");
	cout << "done!" << endl;

	// task3
	cout << "task3......";
	fout.open(".//output//Population.txt", ios::out);
	fout << "年\t大\t小\t中" << endl;

	vector<City>cities_sorted = vector<City>(1023);
	for (int i = 0; i < 10; i++) {
		cities_sorted = era[i].cities;
		sort(cities_sorted.begin(), cities_sorted.end());
		fout << 2010 + i << '\t' << cities_sorted[1022].name << '\t' << cities_sorted[0].name << '\t' << cities_sorted[511].name << endl;
	}
	fout.close();
	cout << "done!" << endl;

	// task4
	cout << "task4......";
	fout.open(".//output//Weather.txt", ios::out);
	fout << "年\t量\t名" << endl;
	for (int i = 0; i < 10; i++) {
		fout << 2010 + i << '\t' << era[i].weather_best() << endl;
	}
	fout.close();
	cout << "done!" << endl;

	// task6
	cout << "task6......\n";
	vector<City>cities_attd_up(1023);
	vector<City>cities_attd_down(1023);

	cities_attd_up = era[0].cities;
	sort(cities_attd_up.begin(), cities_attd_up.end(), cmp_ct_attd_up);

	cities_attd_down = era[0].cities;
	stable_sort(cities_attd_down.begin(), cities_attd_down.end(), greater<City>());

	cout << "sort_up:\t名\t高\t\t\tsort_down:\t名\t高" << endl;
	for (int i = 0; i < 5; i++) {
		cout << "\t\t" << cities_attd_up[i].name << "\t" << cities_attd_up[i].attd << "\t\t\t\t\t" << cities_attd_down[i].name << "\t" << cities_attd_down[i].attd << endl;
	}
	cout << "..." << endl;
	for (int i = 1018; i < 1023; i++) {
		cout << "\t\t" << cities_attd_up[i].name << "\t" << cities_attd_up[i].attd << "\t\t\t\t\t" << cities_attd_down[i].name << "\t" << cities_attd_down[i].attd << endl;
	}
	cout << "task6......done!\n";

	// task5
	cout << "task5......";
	fout.open(".//output//altitude.txt", ios::out);
	fout << "名\t海拔" << endl;
	for (int i = 0; i < 1023; i++) {
		if (cities_attd_up[i].attd > 3500) { break; }
		if (cities_attd_up[i].attd >= 1000)
		{
			fout<< cities_attd_up[i].name << '\t' << cities_attd_up[i].attd << endl;
		}
	}
	fout.close();
	cout << "done!" << endl;

	// task7
	cout << "task7......";
	vector<VirtualCity>air_pre(1023);
	vector<VirtualCity>air_suns;
	vector<VirtualCity>air_popu;
	vector<VirtualCity>air_done;
	vector<VirtualCity>air_port;
	for (int i = 0; i < 1023; i++) {
		air_pre[i].name += era[0].cities[i].name;
		air_pre[i].attd += era[0].cities[i].attd;
		air_pre[i].suns = 0;
	}

	//统计人口数量与好天气数量
	for (int i = 0; i < 10; i++) {
		for (int j = 0; j < 1023; j++) {
			air_pre[j].popu += era[i].cities[j].popu;
			for (int k = 0; k < 12; k++) { if (era[i].cities[j].weathers[k] == Weather::Sunny)air_pre[j].suns++; }
		}
	}

	//分别对好天气数量与人口数量做从大到小排序，并筛选出各自的前100
	sort(air_pre.begin(), air_pre.end(), [](VirtualCity vc1, VirtualCity vc2) {return vc1.suns > vc2.suns; });
	for (int i = 0; i < 100; i++) {
		air_suns.push_back(air_pre[i]);
	}
	sort(air_pre.begin(), air_pre.end(), [](VirtualCity vc1, VirtualCity vc2) {return vc1.popu > vc2.popu; });
	for (int i = 0; i < 100; i++) {
		air_popu.push_back(air_pre[i]);
	}

	// 将符合条件1与条件4的两个集合做交集
	sort(air_suns.begin(), air_suns.end(), [](VirtualCity vc1, VirtualCity vc2) {return vc1.name < vc2.name; });
	sort(air_popu.begin(), air_popu.end(), [](VirtualCity vc1, VirtualCity vc2) {return vc1.name < vc2.name; });
	for (int i = 0, j = 0; i < air_suns.size() && j < air_popu.size(); ) {
		if (air_popu[j].name == air_suns[i].name) {
			air_done.push_back(air_popu[j]);
			i++; j++;
		}
		else if (air_popu[j].name < air_suns[i].name) { j++; }
		else if (air_popu[j].name > air_suns[i].name) { i++; }
	}

	//筛选条件2、3
	if (air_done.size() == 0) { cout << "无城市符合机场条件！" << "......done!"; return 0; }

	int attd_min = air_done[0].attd;
	for (int i = 0; i < air_done.size(); i++) {	if (air_done[i].attd < attd_min) {	attd_min = air_done[i].attd;	}	}
	attd_min += 100;
	for (int i = 0; i < air_done.size(); i++) { if (air_done[i].attd < attd_min) { air_port.push_back(air_done[i]); } }

	if (air_port.size() == 0) { cout << "无城市符合机场条件！" << "......done!"; return 0; }

	fout.open(".//output//Airport.txt", ios::out);
	fout << "名\t天\t高\t人" << endl;
	for (int i = 0; i < air_port.size(); i++) {
		fout << air_port[i].save() << endl;
	}
	fout.close();
	cout << "done!" << endl;

	return 0;

}